﻿using System;
using System.Text.RegularExpressions;
using System.Web;
using Jayden.Dll.Core.Translations;
using Jayden.Dll.Web.Enums;
using Jayden.Dll.Web.HtmlControls.Descriptions;

namespace Jayden.Dll.Web.HtmlControls.Generics
{
	public class HtmlText : HtmlControl
	{
		private string m_Text;
		public HtmlText(string text = "", bool htmlEncode = true)
			: base(HtmlTextDescription.Instance)
		{
			SetText(text, htmlEncode);
		}

		public void SetText(string text, bool htmlEncode = true)
		{
			if (htmlEncode)
				m_Text = HttpUtility.HtmlEncode(text);
			else
				m_Text = text;
		}
		public string GetText(bool htmlDecode = true)
		{
			if(htmlDecode)
				return HttpUtility.HtmlDecode(m_Text);
			return m_Text;
		}
		public string Text { get { return GetText(true); } set { SetText(value, true); } }

		public static string CleanText(string text, RenderMode mode = RenderMode.Production)
		{
			string cleanText = text;
			if (mode != RenderMode.XHtml && cleanText != null && cleanText.Length > 0)
			{
				// remove leading tabs and carriage return (html would have replaced them with space)
				// this is an internal convention, just deal with it !
				// xml document autoformat should be configured to use tab and not space !
				cleanText = cleanText.Trim('\r', '\n', '\t');
				// replace tabs and carriage return by spaces
				cleanText = cleanText.Replace("\r", " ").Replace("\n", " ").Replace("\t", " ");
				// replace multiple space by single space (html display multiple space as single space anyway)
				Regex regex = new Regex("[ ]+");
				cleanText = regex.Replace(cleanText, " ");
			}
			return cleanText;
		}

		public override void ProcessHtmlRequest(HttpHandlerContext context) { }
		public override void ProcessJsonRequest(HttpHandlerContext context) { }
		public override void Render(System.Web.UI.HtmlTextWriter writer, RenderMode mode, int level, bool innerHtml = false)
		{
			if (innerHtml)
				return;
			// the value of m_Text should always be html encoded
			if(mode != RenderMode.Production)
			{
				for (int c = 0; c < level; c++)
					writer.Write('\t');
				if(level > 0 && mode == RenderMode.Debug)
					writer.Write("-->");
			}
			string cleanText = CleanText(m_Text, mode);
			// the text should already be html encoded at this point
			writer.Write(cleanText);
			if (level != 0 && (mode == RenderMode.Debug || mode == RenderMode.XHtml))
			{
				if (mode == RenderMode.Debug)
					writer.Write("<!--");
				writer.WriteLine();
			}
		}
		public override string ToString() { return m_Text; }

		public override void Load(System.Xml.XmlNode node, HtmlControlLoader loader) { m_Text = node.InnerText; }

		public override void TranslateControl(ITranslator translator) { m_Text = translator.Translate(CleanText(m_Text)); }

	}
	public class HtmlTextDescription : HtmlControlDescription
	{
		public static HtmlTextDescription Instance = new HtmlTextDescription();
		private HtmlTextDescription() { }
		public override Type ControlType { get { return typeof(HtmlText); } }
	}
}
